<?php
namespace service;

use service\SystemConfigService;
use think\Cache;
vendor('ks3phpsdk.Ks3Client');

/** 手动引入ksy3client 20201030 by fw */
require_once 'vendor/ks3phpsdk/Ks3Client.class.php';

class JSYService
{
    protected static $bucketName='mini-program';

    protected static $region='SHANGHAI';

    const KS3_HOST='ks3-cn-shanghai.ksyuncs.com';
    const FILE_DOMAIN = 'https://mini-program.ks3-cn-shanghai.ksyuncs.com/';

    protected static $client=null;

    protected static $access_key_id=null;

    protected static $access_sk=null;

    protected static function init(){
        $confing=[
            'access_key_id' => \think\Env::get('JSCLOUD.ACCESS_KEY_ID'),
            'access_sk'     => \think\Env::get('JSCLOUD.ACCESS_SECRET'),
        ];

        if(!isset($confing['access_key_id']) || !isset($confing['access_sk'])) exception('请先配置金山云ID和Sk');
        self::$access_key_id=$confing['access_key_id'];
        self::$access_sk=$confing['access_sk'];
        if(self::$client===null)
            self::$client = new \Ks3Client($confing['access_key_id'],$confing['access_sk'], self::KS3_HOST);

        return self::$client;
    }

    public static function getAccessKeyId()
    {
        return self::$access_key_id;
    }
    public static function getAccessSk(){
        return self::$access_sk;
    }
    public static function iso8601($time=false) {
        if ($time === false) $time = time();
        $date = date('Y-m-d\TH:i:s\.Z', $time);
        return (substr($date, 0, strlen($date)-2).'Z');
    }
    public static function getFileList($page=1,$limit=20,$folderName='image'){
        $args =[
            "Bucket" => self::$bucketName,
            "Options"=>[
                "prefix"=>$folderName."/",
                "max-keys"=>$page*$limit,
                "delimiter"=>"<>",
            ]
        ];
        return self::init()->listObjects($args);
    }
    /**
     * 检测是否含有中文
     * @param string $subject
     * @return boolean
     */
    public static function chk_chinese($str){
        return preg_match('/[\x80-\xff]./', $str);
    }

    /**
     * 上传文件
     * @Author   FW
     * @DateTime 2024-05-07
     * @Email    123763712@qq.com
     * @param    [type]           $file [description]
     * @param    [type]           $name [description]
     * @return   [type]                 [description]
     */
    public static function upLoaderFile($file,$name,$contentType = 'image/jpg'){
        if(self::chk_chinese($file)) $file = iconv('utf-8','gbk',$file);
        $args = [
            "Bucket"=>self::$bucketName,
            "Key"=>$name,
            "ACL"=>"public-read",
            "ObjectMeta"=>[
                "Content-Type" => $contentType,//只传0-10字节,
            ],
            "Content"=>[
                "content"=>$file,
                "seek_position"=>0
            ],
        ];
        return self::init()->putObjectByFile($args);
    }
    /*
     * 删除指定资源
     * @param string $key 资源名称
     * @return array
     * */
    public static function DelectFile($key){
        if(empty($key)) return false;
        $args =[
            "Bucket"=>self::$bucketName,
            "Key"=>$key
        ];
        return self::init()->deleteObject($args);
    }
    /*
     * 图片格式
     * @param int $format 1=大图,2=宫图,3=全图,4=小图
     * @return string
     * */
    public static function getImageFormat($format=1){
        $type='';
        switch ($format){
            case 1:
                $type='?tag=imgScale&f=1&c=1&q=96&w=690&h=254&r=0&rotate=0';
                break;
            case 2:
                $type='?tag=imgScale&f=1&c=1&q=100&w=330&h=200&r=0&rotate=0';
                break;
            case 4:
                $type='?tag=imgScale&f=1&c=1&q=100&w=690&h=254&r=0&rotate=0';
                break;
            case 3:
                $type='?tag=imgScale&f=1&c=1&q=100&w=219&h=130&r=0&rotate=0';
                break;
        }
        return $type;
    }
}